package com.ailk.evaluation.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.anhorn.webshare.base.dao.BaseJdbcDao;




public class JobDao extends BaseJdbcDao{

	
	/**
	 * @param minId 
	 * @return
	 */
	public List<Map> findMailJob() {
		String sql = "select e.evaluation_id, ee.evaluation_sub_id, e.evaluation_name, s.supplier_id, s.supplier_name, ee.email " +
				" from EVALUATION e, EVALUATION_ITEM ei, EVALUATION_EVALUATOR ee, SUPPLIER s " +
				" where e.object_type_id = '1' and ei.evaluation_id = e.evaluation_id " +
				" and ei.attr_code = 'DATE' and ei.attr_value1 = '2' " +
				" and ee.evaluation_id = e.evaluation_id and s.supplier_id = e.object_id " +
				" and ee.send_tag ='0' ";
		return this.getJdbcTemplate().queryForList(sql);
	}
    
	public List<Map> findUnInitMailJob() {
		String sql = " select t.* from EVALUATION_ITEM t where t.attr_value1 = MONTH(SYSDATE()) and attr_code ='EVALUATOR' " +
				" and NOT exists (select 1 from EVALUATION_EVALUATOR s where t.attr_value2 = s.EMAIL and t.evaluation_id=s.evaluation_id ); ";
		return this.getJdbcTemplate().queryForList(sql);
	}
	
	/**
	 * @param userId
	 * @param history
	 * @param i
	 */
	public void initMailJob(List<Map> evaluators) {
		final int size = evaluators.size();
		final  List<Map> evaluator = evaluators;
		String sql = "insert into EVALUATION_EVALUATOR(EVALUATION_ID,EMAIL,SEND_TAG) values(?,?,'0')";
		//this.getJdbcTemplate().update(sql, new Object[]{email, date, date2});
		/*this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public int getBatchSize() {
				return size;
			}
			@Override
			public void setValues(PreparedStatement preparedStatement, int i)
					throws SQLException {
				preparedStatement.setInt(1, (Integer)(evaluator.get(i)).get("EVALUATION_ID"));
				preparedStatement.setString(2, (String)(evaluator.get(i)).get("ATTR_VALUE2"));
			}
		});*/
	}
	/**
	 * @param userId
	 * @param date 
	 */
	public void updateMailState(int send_tag ,String email,String result_info,int evaluation_id) {
		String sql = "update EVALUATION_EVALUATOR set send_tag = ? ,send_time = ? ,RESULT_INFO = ? where email = ? and send_tag <> '1' and evaluation_id =?";
		
		this.getJdbcTemplate().update(sql, new Object[]{send_tag,new Date(),result_info, email,evaluation_id});		
	}
}
